/**
 * 
 */
package com.card.client.dialog;

import com.card.client.entry.FinanceType;
import com.card.client.entry.Util;
import com.card.client.service.FinanceTypeConfigService;
import com.card.client.service.FinanceTypeConfigServiceAsync;
import com.card.client.view.FinanceTypeConfigView;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

/**
 * @author wangx
 *
 */
public class EditFinanceTypeConfigDialog extends DialogBox {

	private static EditFinanceTypeConfigDialogUiBinder uiBinder = GWT
			.create(EditFinanceTypeConfigDialogUiBinder.class);

	interface EditFinanceTypeConfigDialogUiBinder extends
			UiBinder<Widget, EditFinanceTypeConfigDialog> {
	}

	private final FinanceTypeConfigServiceAsync fts = GWT.create(FinanceTypeConfigService.class);
	
	private Long id;
	
	@UiField TextBox financeType;
	
	@UiField RadioButton cash;
	
	@UiField RadioButton nonCash;
	
	@UiField RadioButton amountIn;
	
	@UiField RadioButton amountOut;
	
	@UiField TextBox financeTypeNotes;
	
	@UiField Button submit;
	
	@UiField Button closeDialog;
	
	@UiField Label message;
	
	FinanceTypeConfigView financeTypeConfigView;

	FinanceType fType;
	
	public EditFinanceTypeConfigDialog(String title, String descript, FinanceTypeConfigView financeTypeConfigView, FinanceType fType, int flag) {
		setWidget(uiBinder.createAndBindUi(this));
		setTitle(title);
		setText(descript);
		setGlassEnabled(true);
		setAnimationEnabled(true);
		setModal(true);
		message.setStyleName("descript");
		financeType.setMaxLength(Util.INPUT_TEXT_MAX_LENGTH);
		financeTypeNotes.setMaxLength(Util.INPUT_TEXT_MAX_LENGTH * 3);
		center();
		this.financeTypeConfigView = financeTypeConfigView;
		submit.setText("添加");
		if(flag == Util.DIALOG_CONTROL_EDIT){
			submit.setText("修改");
			this.fType = fType;
			id = fType.getId();
			financeType.setText(fType.getFinanceType());
			if(fType.getTypeIndex() == Util.FINANCE_TYPE_IS_CASH) {
				cash.setValue(true);
				nonCash.setValue(false);
			} else if (fType.getTypeIndex() == Util.FINANCE_TYPE_IS_CARD) {
				cash.setValue(false);
				nonCash.setValue(true);
			}
			if(fType.getTypeInOut() == Util.FINANCE_TYPE_AMOUNT_IN) {
				amountIn.setValue(true);
				amountOut.setValue(false);
			} else if(fType.getTypeInOut() == Util.FINANCE_TYPE_AMOUNT_OUT) {
				amountIn.setValue(false);
				amountOut.setValue(true);
			}
			financeTypeNotes.setText(fType.getFinanceTypeNotes());
		}
	}

	@UiHandler("submit")
	void onClickSubmit(ClickEvent e) {
		if(!isValid()){
			return;
		}
		FinanceType ft = new FinanceType();
		ft.setId(id);
		ft.setFinanceType(financeType.getText());
		if(cash.getValue()){
			ft.setTypeIndex(Util.FINANCE_TYPE_IS_CASH);
		} else if (nonCash.getValue()) {
			ft.setTypeIndex(Util.FINANCE_TYPE_IS_CARD);
		}
		if(amountIn.getValue()) {
			ft.setTypeInOut(Util.FINANCE_TYPE_AMOUNT_IN);
		} else if(amountOut.getValue()) {
			ft.setTypeInOut(Util.FINANCE_TYPE_AMOUNT_OUT);
		}
		ft.setFinanceTypeNotes(financeTypeNotes.getText());
		if(null != id) {
			fts.updateFinanceType(ft, new AsyncCallback<Void>() {
				public void onSuccess(Void result) {
					hide();
					financeTypeConfigView.onLoad();
				}
				public void onFailure(Throwable caught) {}
			});
		} else {
			fts.addFinanceType(ft, new AsyncCallback<Void>() {
				public void onSuccess(Void result) {
					hide();
					financeTypeConfigView.onLoad();
				}
				public void onFailure(Throwable caught) {}
			});
		}
	}
	
	@UiHandler("closeDialog")
	void onClickCloseDialog(ClickEvent e) {
		hide();
	}

	private void setMessage(String msg){
		message.setText(msg);
	}
	
	private boolean isValid(){
		setMessage("");
		/**
		 * @UiField TextBox financeType;
	@UiField RadioButton cash;
	@UiField RadioButton nonCash;
	@UiField RadioButton amountIn;
	@UiField RadioButton amountOut;
	@UiField TextBox financeTypeNotes;
		 * */
		String check = Util.validInput(financeType.getText());
		if(null != check){
			financeType.setFocus(true);
			setMessage("类型名称" + check);
			return false;
		}
		if(!(cash.getValue() || nonCash.getValue())){
			cash.setFocus(true);
			setMessage("没有选择种类");
			return false;
		}
		if(!(amountIn.getValue() || amountOut.getValue())){
			amountIn.setFocus(true);
			setMessage("没有选择收入支出");
			return false;
		}
		return true;
	}
}
